package com.gali.mall.controller;

import com.gali.mall.common.ResultInfo;
import com.gali.mall.exception.GaLiExceptionEnum;
import com.gali.mall.model.pojo.Category;
import com.gali.mall.model.vo.CategoryVO;
import com.gali.mall.request.categoryReq.AddCategoryReq;
import com.gali.mall.request.categoryReq.SelectCategoryReq;
import com.gali.mall.request.categoryReq.UpdateCategoryReq;
import com.gali.mall.service.CategoryService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.List;


@RestController
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 增加商品
     * @param addCategoryReq
     * @param session
     * @return
     */
    @PostMapping("/category/admin/add")
     public ResultInfo addCategory(@RequestBody @Valid AddCategoryReq addCategoryReq, HttpSession session){
         Boolean flag = categoryService.addCategory(addCategoryReq);
         if (flag){
             return ResultInfo.success();
         }
         return ResultInfo.error(GaLiExceptionEnum.ADD_ERROR);
     }

    /**
     * 删除商品
     * @param id
     * @param session
     * @return
     */
     @PostMapping("/category/admin/delete")
     public ResultInfo deleteCategory(@RequestParam("id") Integer id, HttpSession session){
         if (id>0){
             Boolean flag = categoryService.deleteCategory(id);
             if (flag){
                 return ResultInfo.success();
             }
             return ResultInfo.error(GaLiExceptionEnum.DELETE_ERROR);
         }
         return ResultInfo.error(GaLiExceptionEnum.FORMAT_ERROR);
     }

    /**
     * 更新商品
     * @param updateCategoryReq
     * @param session
     * @return
     */
     @PostMapping("/category/admin/update")
     public ResultInfo updateCategory(@RequestBody @Valid UpdateCategoryReq updateCategoryReq,HttpSession session){
         Boolean flag = categoryService.updateCategory(updateCategoryReq);
         if (flag){
             return ResultInfo.success();
         }
         return ResultInfo.error(GaLiExceptionEnum.UPDATE_ERROR);
     }

    /**
     * 查询全部商品
     * @return
     */
     @GetMapping("/category/findAll")
     public ResultInfo findAll(){
         List<Category> all = categoryService.findAll();
         if (all.size()>0){
             return ResultInfo.success(all);
         }
         return ResultInfo.error(GaLiExceptionEnum.SELECT_ERROR);
     }

    /**
     * 根据商品名字模糊查询
     * @param selectCategoryReq
     * @return
     */
     @PostMapping("/category/findFromName")
     public ResultInfo findByName(@RequestBody SelectCategoryReq selectCategoryReq){
         String name = selectCategoryReq.getName();
         List<Category> categoryFromName = categoryService.findCategoryFromName("%" + name + "%");
         if (categoryFromName.size()>0){
             return ResultInfo.success(categoryFromName);
         }
         return ResultInfo.error(GaLiExceptionEnum.SELECT_ERROR);
     }

    /**
     * 通过id查询商品
     * @param selectCategoryReq
     * @return
     */
     @PostMapping("/category/findById")
     public ResultInfo findById(@RequestBody SelectCategoryReq selectCategoryReq){
         Category categoryById = categoryService.findCategoryById(selectCategoryReq.getId());
         if (categoryById!=null){
             return ResultInfo.success(categoryById);
         }
         return ResultInfo.error(GaLiExceptionEnum.SELECT_ERROR);
     }

    /**
     * 通过type查询商品
     * @param selectCategoryReq
     * @return
     */
     @PostMapping("/category/findByType")
     public ResultInfo findByType(@RequestBody SelectCategoryReq selectCategoryReq){
         List<Category> categoryByType = categoryService.findCategoryByType(selectCategoryReq.getType());
         if (categoryByType.size()>0){
             return ResultInfo.success(categoryByType);
         }
         return ResultInfo.error(GaLiExceptionEnum.SELECT_ERROR);
     }

    /**
     * 分页查询（管理员）
     * @param pageNumber
     * @param pageSize
     * @return
     */
     @GetMapping("/category/admin/findForPage")
     public ResultInfo selectForPage(@RequestParam("pageNumber") Integer pageNumber,@RequestParam("pageSize") Integer pageSize){
         if (pageNumber<0||pageSize<0){
             return ResultInfo.error(GaLiExceptionEnum.SELECT_ERROR);
         }
         PageInfo pageInfo = categoryService.listForAdmin(pageNumber, pageSize);
         return ResultInfo.success(pageInfo);
     }

    /**
     * 分页查询（用户）
     * @return
     */
     @GetMapping("/category/findForConsumer")
     public ResultInfo selectForConsumer(){
         List<CategoryVO> categoryVoList = categoryService.listForCustomer(0);
         return ResultInfo.success(categoryVoList);
     }
}
